import { Mesh, Object3D } from "three";
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'

export default class ModelLoader {

    gltfLoader: GLTFLoader = new GLTFLoader();

    constructor() {

    }

    /**
 * ---------------- 加载模型 ---------------------
 * @param {*} path // 模型加载路径
 * @param {*} scale // 模型初始化大小
 * @returns 
 */
    loadModel(path: string, scale?: [number, number, number]): Promise<Object3D> {
        return new Promise((resolve, reject) => {
            this.gltfLoader.load(
                path,
                (gltf) => {
                    this.adjustModel(gltf.scene, scale);
                    resolve(gltf.scene)
                },
                function (xhr) {
                    console.log(xhr.loaded / xhr.total, "模型加载进度");
                },
            )
        });
    }

    adjustModel(scene: Object3D, scale = [1, 1, 1], metalness = 0.1) {
        scene.scale.set(scale[0], scale[1], scale[2]);
        scene.traverse(function (child) {
            if (child instanceof Mesh) {
                child.material.metalness = metalness;
            }
        })
    }

}

